package com.zisuyi.uilibrary.module.activty.login

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import com.weilele.mvvm.base.helper.LiveDataWrap
import com.weilele.mvvm.base.helper.StatusValue
import com.weilele.mvvm.base.helper.annotation.MvvmViewModel
import com.weilele.mvvm.base.helper.appCompatActivity
import com.weilele.mvvm.base.helper.wrapObserver
import com.weilele.mvvm.utils.activity.navigateTo
import com.weilele.mvvm.utils.activity.toast
import com.weilele.mvvm.utils.file.kvDecode
import com.weilele.mvvm.utils.file.kvEncode
import com.zisuyi.game.sdk.library.login.IGameLogin
import com.zisuyi.game.sdk.library.login.LoginType
import com.zisuyi.game.sdk.net.bean.UserInfoBean
import com.zisuyi.uilibrary.R
import com.zisuyi.uilibrary.base.BaseUiActivity
import com.zisuyi.uilibrary.base.BaseUiGameActivity
import com.zisuyi.uilibrary.databinding.ActivityLoginBinding
import com.zisuyi.uilibrary.module.activty.home.HomeActivity


class LoginActivity : BaseUiGameActivity<ActivityLoginBinding>() {
    companion object {
        private const val KEV_LAST_LOGIN_TYPE = "KEV_LAST_LOGIN_TYPE"
        fun start(appCompatActivity: AppCompatActivity?) {
            appCompatActivity.navigateTo<LoginActivity>()
            appCompatActivity?.finish()
        }

        fun isAutoGoogleLogin(): Boolean {
            return kvDecode(KEV_LAST_LOGIN_TYPE, "") == IGameLogin.LOGIN_GOOGLE
        }
    }

    private val googleLogin by lazy {
        IGameLogin(
                supportFragmentManager,
                this,
                IGameLogin.LOGIN_GOOGLE
        )
    }
    private val facebookLogin by lazy {
        IGameLogin(
                supportFragmentManager,
                this,
                IGameLogin.LOGIN_FACEBOOK
        )
    }


    @MvvmViewModel
    private lateinit var userLoginViewModel: UserLoginViewModel

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        googleLogin.addOnLoginListener(onLoginListener)
        facebookLogin.addOnLoginListener(onLoginListener)
    }

    override fun initData() {
        super.initData()
        if (isAutoGoogleLogin()) {
            googleLogin.requestLogin()
        }
    }

    override fun getClickView(): List<View?>? {
        return listOf(mBinding.btLoginFb, mBinding.btLoginGoogle)
    }

    override fun onSingleClick(view: View) {
        super.onSingleClick(view)
        when (view) {
            mBinding.btLoginFb -> {
                showLoadingView()
                facebookLogin.requestLogin()
            }
            mBinding.btLoginGoogle -> {
                showLoadingView()
                googleLogin.requestLogin()
            }
        }
    }

    override fun isNeedSwitchView(): Boolean {
        return true
    }

    private var userInfo: IGameLogin.UserInfo? = null
    private val onLoginListener = object : IGameLogin.OnLoginListener {
        override fun onSuccess(info: IGameLogin.UserInfo, type: String) {
            userInfo = info
            userLoginViewModel.userLogin(info)
        }

        override fun onCancel(type: String) {
            showContentView()
            "Login Cancel".toast()
        }

        override fun onError(error: Throwable, type: String) {
            showContentView()
            "Login Error:${error.message}".toast()
        }
    }

    override fun getObserverLiveData(): List<LiveDataWrap>? {
        return listOf(userLoginViewModel.userInfoData wrapObserver ::onUserLogin)
    }

    private fun onUserLogin(value: StatusValue<UserInfoBean>?) {
        when (value) {
            is StatusValue.Running -> {
                showLoadingView("Logging in...")
            }
            is StatusValue.Error -> {
                val info = userInfo
                if (info == null) {
                    value.nonNullMsg.toast()
                    showContentView()
                } else {
                    showOtherView(value.nonNullMsg) {
                        showLoadingView("Logging in...")
                        userLoginViewModel.userLogin(info)
                    }
                }
            }

            is StatusValue.Success -> {
                showLoadingView("Loading Game...")
                loadGame()
                kvEncode(KEV_LAST_LOGIN_TYPE, userInfo?.type)
            }
        }
    }
}